package cn.xqapp.TimeClient;

import cn.xqapp.core.BaseClient;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;

public class TimeClient extends BaseClient {
    public TimeClient(String host, int port){
        super(host, port, ()-> new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) {
                ch.pipeline().addLast(new TimeDecoder(), new TimeClientHandler());
            }
        });
    }

    @Override
    public void run() {
        Runnable task = ()->super.run();
        for(int i=0; i<10; i++){
            long taskTime = stopWatch(task);
            System.out.println(String.format("task %d finished in %d millionsecnods", i+1, taskTime));
        }
    }

    protected long stopWatch(Runnable runnable){
        long startTime = System.currentTimeMillis();
        runnable.run();
        return System.currentTimeMillis()-startTime;
    }
}
